{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b''"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "# 1.创建人脸列表\n",
    "faceListId = \"list_004\" # 设置人脸列表ID\n",
    "create_facelists_url =\"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}\" # URL（见API文档）\n",
    "subscription_key =\"f212f71663134b73bdec4a5a9aece2f0\" # 密钥\n",
    "# 请求头\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 相关信息及识别模型的选用\n",
    "data = {\n",
    "  \"name\": \"sample_list\",\n",
    "  \"userData\": \"相册\",\n",
    "  \"recognitionModel\": \"recognition_03\"\n",
    "}\n",
    "# 发送请求\n",
    "r_create = requests.put(create_facelists_url.format(faceListId), headers=headers, json=data)\n",
    "r_create.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.添加人脸\n",
    "add_face_url =\"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 人脸相片地址\n",
    "img_url =\"https://www.apple.com.cn/newsroom/images/live-action/keynote-september-2020/apple_apple-event-keynote_tim_09152020_big.jpg.large_2x.jpg\"\n",
    "# 人脸数据\n",
    "params_add_face={\n",
    "    \"userData\":\"Tim Cook\"\n",
    "}\n",
    "# 发送请求\n",
    "r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "r_add_face.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaceId': '04152fc3-7ded-459f-8f93-35db58f2a41a'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_add_face.json() # 返回persistedFaceId"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装成函数方便添加图片\n",
    "def AddFace(img_url=str,userData=str):\n",
    "    add_face_url =\"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedFaces\"\n",
    "    assert subscription_key\n",
    "    headers = {\n",
    "        # Request headers\n",
    "        'Content-Type': 'application/json',\n",
    "        'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "    }\n",
    "    img_url = img_url\n",
    "    params_add_face={\n",
    "        \"userData\":userData\n",
    "    }\n",
    "    r_add_face = requests.post(add_face_url.format(faceListId),headers=headers,params=params_add_face,json={\"url\":img_url})\n",
    "    return r_add_face.status_code #返回出状态码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Autumnhui.jpg\",\"丘天惠\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/L-Tony-info.jpg\",\"林嘉茵\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/TLINGP.jpg\",\"汤玲萍\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/WenYanZeng.jpg\",\"曾雯燕\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/XIEIC.jpg\",\"谢依希\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/YuecongYang.png\",\"杨悦聪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/Zoezhouyu.jpg\",\"周雨\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/crayon-heimi.jpg\",\"刘瑜鹏\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/jiayichen.jpg\",\"陈嘉仪\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/kg2000.jpg\",\"徐旖芊\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuxinrujiayou.jpg\",\"刘心如\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/liuyu19.png\",\"刘宇\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/ltco.jpg\",\"李婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lucaszy.jpg\",\"黄智毅\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/pingzi0211.jpg\",\"黄慧文\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/shmimy-cn.jpg\",\"张铭睿\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/yichenting.jpg\",\"陈婷\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/coco022.jpg\",\"洪可凡\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/lujizhi.png\",\"卢继志\")\n",
    "AddFace(\"http://huangjieqi.gitee.io/picture_storage/zzlhyy.jpg\",\"张梓乐\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '036c5db1-83d5-4d21-8917-d33294080b39',\n",
       "  'faceRectangle': {'top': 121, 'left': 571, 'width': 146, 'height': 146},\n",
       "  'faceAttributes': {'smile': 0.005,\n",
       "   'headPose': {'pitch': 1.3, 'roll': -7.5, 'yaw': -7.0},\n",
       "   'gender': 'male',\n",
       "   'age': 60.0,\n",
       "   'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "   'glasses': 'ReadingGlasses',\n",
       "   'emotion': {'anger': 0.003,\n",
       "    'contempt': 0.002,\n",
       "    'disgust': 0.002,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.005,\n",
       "    'neutral': 0.986,\n",
       "    'sadness': 0.001,\n",
       "    'surprise': 0.0},\n",
       "   'blur': {'blurLevel': 'low', 'value': 0.16},\n",
       "   'exposure': {'exposureLevel': 'goodExposure', 'value': 0.67},\n",
       "   'noise': {'noiseLevel': 'low', 'value': 0.0},\n",
       "   'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "   'accessories': [{'type': 'glasses', 'confidence': 0.99}],\n",
       "   'occlusion': {'foreheadOccluded': False,\n",
       "    'eyeOccluded': False,\n",
       "    'mouthOccluded': False},\n",
       "   'hair': {'bald': 0.44,\n",
       "    'invisible': False,\n",
       "    'hairColor': [{'color': 'gray', 'confidence': 1.0},\n",
       "     {'color': 'blond', 'confidence': 0.73},\n",
       "     {'color': 'other', 'confidence': 0.67},\n",
       "     {'color': 'black', 'confidence': 0.57},\n",
       "     {'color': 'brown', 'confidence': 0.03},\n",
       "     {'color': 'red', 'confidence': 0.01},\n",
       "     {'color': 'white', 'confidence': 0.0}]}}}]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.检测人脸\n",
    "face_api_url = 'https://api-hjq.cognitiveservices.azure.com/face/v1.0/detect'\n",
    "image_url = 'https://www.apple.com.cn/newsroom/images/live-action/keynote-september-2020/apple_apple-event-keynote_tim_09152020_big.jpg.large_2x.jpg'\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "# 请求参数\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 选择模型\n",
    "    'recognitionModel':'recognition_03',#此参数需与facelist参数一致\n",
    "    'detectionModel':'detection_01',\n",
    "    # 可选参数，请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "response = requests.post(face_api_url, params=params,headers=headers, json={\"url\": image_url})\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'persistedFaceId': '04152fc3-7ded-459f-8f93-35db58f2a41a',\n",
       "  'confidence': 1.0},\n",
       " {'persistedFaceId': 'fb63b4b0-fb8d-4c6b-afc9-35951c291c9b',\n",
       "  'confidence': 0.0995},\n",
       " {'persistedFaceId': 'a06c9e26-783b-4074-af26-01c531ef61da',\n",
       "  'confidence': 0.09931},\n",
       " {'persistedFaceId': 'de49fea3-c4ea-4fa8-baf3-7dffa7fd4887',\n",
       "  'confidence': 0.09631},\n",
       " {'persistedFaceId': '174f9dfa-66e3-4f32-a25e-9b0d69ad61ae',\n",
       "  'confidence': 0.0949},\n",
       " {'persistedFaceId': '0229d72c-8cc1-4d2d-aca2-f39f1084864e',\n",
       "  'confidence': 0.09432},\n",
       " {'persistedFaceId': 'c2c37fec-623a-4b07-8509-bf0cfa02bbca',\n",
       "  'confidence': 0.0926},\n",
       " {'persistedFaceId': '1e4d1356-1499-4ca1-bc24-9d33af9e7200',\n",
       "  'confidence': 0.09259},\n",
       " {'persistedFaceId': '0bcead85-3eda-456f-aa4d-518f7155c718',\n",
       "  'confidence': 0.09235},\n",
       " {'persistedFaceId': 'bd162714-85c3-4485-8b5a-e557e8bc02a4',\n",
       "  'confidence': 0.09184}]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.返回人脸相似置信度\n",
    "findsimilars_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/findsimilars\"\n",
    "# 请求正文 faceID需要先检测一张照片获取\n",
    "data_findsimilars = {\n",
    "    \"faceId\":\"036c5db1-83d5-4d21-8917-d33294080b39\", #取上方的faceID\n",
    "    \"faceListId\": \"list_004\",\n",
    "    \"maxNumOfCandidatesReturned\": 10,\n",
    "    \"mode\": \"matchFace\" #matchPerson #一种为验证模式，一种为相似值模式\n",
    "    }\n",
    "r_findsimilars = requests.post(findsimilars_url,headers=headers,json=data_findsimilars)\n",
    "r_findsimilars.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'persistedFaces': [{'persistedFaceId': '04152fc3-7ded-459f-8f93-35db58f2a41a',\n",
       "   'userData': 'Tim Cook'},\n",
       "  {'persistedFaceId': '174f9dfa-66e3-4f32-a25e-9b0d69ad61ae',\n",
       "   'userData': '丘天惠'},\n",
       "  {'persistedFaceId': '0229d72c-8cc1-4d2d-aca2-f39f1084864e',\n",
       "   'userData': '林嘉茵'},\n",
       "  {'persistedFaceId': 'fb63b4b0-fb8d-4c6b-afc9-35951c291c9b',\n",
       "   'userData': '汤玲萍'},\n",
       "  {'persistedFaceId': 'd6a180f3-407e-4b20-aae3-5faa0f20dd90',\n",
       "   'userData': '曾雯燕'},\n",
       "  {'persistedFaceId': '92ac3faf-2474-456c-a413-602fa1988bbd',\n",
       "   'userData': '谢依希'},\n",
       "  {'persistedFaceId': 'a06c9e26-783b-4074-af26-01c531ef61da',\n",
       "   'userData': '杨悦聪'},\n",
       "  {'persistedFaceId': '8445b7db-9d62-4dce-9d90-edd1f3569dc6',\n",
       "   'userData': '周雨'},\n",
       "  {'persistedFaceId': '216e1b61-e496-472b-bb75-689cf2f4b0f1',\n",
       "   'userData': '刘瑜鹏'},\n",
       "  {'persistedFaceId': '524e19de-6602-4e3d-a619-42dbf9f74714',\n",
       "   'userData': '陈嘉仪'},\n",
       "  {'persistedFaceId': '0bcead85-3eda-456f-aa4d-518f7155c718',\n",
       "   'userData': '徐旖芊'},\n",
       "  {'persistedFaceId': 'de49fea3-c4ea-4fa8-baf3-7dffa7fd4887',\n",
       "   'userData': '刘心如'},\n",
       "  {'persistedFaceId': 'c2c37fec-623a-4b07-8509-bf0cfa02bbca',\n",
       "   'userData': '刘宇'},\n",
       "  {'persistedFaceId': 'bd162714-85c3-4485-8b5a-e557e8bc02a4',\n",
       "   'userData': '李婷'},\n",
       "  {'persistedFaceId': 'cac7ccd1-8b36-4957-ae4e-76a26c285ae1',\n",
       "   'userData': '黄智毅'},\n",
       "  {'persistedFaceId': '5adb05cd-466e-44b4-8f7b-70003db4e7d5',\n",
       "   'userData': '黄慧文'},\n",
       "  {'persistedFaceId': 'ebc6190f-d734-4627-abeb-b743cffdfa81',\n",
       "   'userData': '张铭睿'},\n",
       "  {'persistedFaceId': '742ad08f-725c-4fb1-92b8-176565de4766',\n",
       "   'userData': '陈婷'},\n",
       "  {'persistedFaceId': '7ae16792-26e9-439f-b348-cc92ee8eac52',\n",
       "   'userData': '洪可凡'},\n",
       "  {'persistedFaceId': '1e4d1356-1499-4ca1-bc24-9d33af9e7200',\n",
       "   'userData': '卢继志'},\n",
       "  {'persistedFaceId': '5d2a20c7-9500-4e31-9d55-5d54a4d417ff',\n",
       "   'userData': '张梓乐'}],\n",
       " 'faceListId': 'list_004',\n",
       " 'name': 'sample_list',\n",
       " 'userData': '相册'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看列表\n",
    "get_facelist_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "r_get_facelist = requests.get(get_facelist_url.format(faceListId),headers=headers)\n",
    "r_get_facelist.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>04152fc3-7ded-459f-8f93-35db58f2a41a</td>\n",
       "      <td>Tim Cook</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>174f9dfa-66e3-4f32-a25e-9b0d69ad61ae</td>\n",
       "      <td>丘天惠</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0229d72c-8cc1-4d2d-aca2-f39f1084864e</td>\n",
       "      <td>林嘉茵</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>fb63b4b0-fb8d-4c6b-afc9-35951c291c9b</td>\n",
       "      <td>汤玲萍</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>d6a180f3-407e-4b20-aae3-5faa0f20dd90</td>\n",
       "      <td>曾雯燕</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>92ac3faf-2474-456c-a413-602fa1988bbd</td>\n",
       "      <td>谢依希</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>a06c9e26-783b-4074-af26-01c531ef61da</td>\n",
       "      <td>杨悦聪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8445b7db-9d62-4dce-9d90-edd1f3569dc6</td>\n",
       "      <td>周雨</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>216e1b61-e496-472b-bb75-689cf2f4b0f1</td>\n",
       "      <td>刘瑜鹏</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>524e19de-6602-4e3d-a619-42dbf9f74714</td>\n",
       "      <td>陈嘉仪</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0bcead85-3eda-456f-aa4d-518f7155c718</td>\n",
       "      <td>徐旖芊</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>de49fea3-c4ea-4fa8-baf3-7dffa7fd4887</td>\n",
       "      <td>刘心如</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>c2c37fec-623a-4b07-8509-bf0cfa02bbca</td>\n",
       "      <td>刘宇</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>bd162714-85c3-4485-8b5a-e557e8bc02a4</td>\n",
       "      <td>李婷</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>cac7ccd1-8b36-4957-ae4e-76a26c285ae1</td>\n",
       "      <td>黄智毅</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>5adb05cd-466e-44b4-8f7b-70003db4e7d5</td>\n",
       "      <td>黄慧文</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>ebc6190f-d734-4627-abeb-b743cffdfa81</td>\n",
       "      <td>张铭睿</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>742ad08f-725c-4fb1-92b8-176565de4766</td>\n",
       "      <td>陈婷</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>7ae16792-26e9-439f-b348-cc92ee8eac52</td>\n",
       "      <td>洪可凡</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1e4d1356-1499-4ca1-bc24-9d33af9e7200</td>\n",
       "      <td>卢继志</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>5d2a20c7-9500-4e31-9d55-5d54a4d417ff</td>\n",
       "      <td>张梓乐</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         persistedFaceId  userData\n",
       "0   04152fc3-7ded-459f-8f93-35db58f2a41a  Tim Cook\n",
       "1   174f9dfa-66e3-4f32-a25e-9b0d69ad61ae       丘天惠\n",
       "2   0229d72c-8cc1-4d2d-aca2-f39f1084864e       林嘉茵\n",
       "3   fb63b4b0-fb8d-4c6b-afc9-35951c291c9b       汤玲萍\n",
       "4   d6a180f3-407e-4b20-aae3-5faa0f20dd90       曾雯燕\n",
       "5   92ac3faf-2474-456c-a413-602fa1988bbd       谢依希\n",
       "6   a06c9e26-783b-4074-af26-01c531ef61da       杨悦聪\n",
       "7   8445b7db-9d62-4dce-9d90-edd1f3569dc6        周雨\n",
       "8   216e1b61-e496-472b-bb75-689cf2f4b0f1       刘瑜鹏\n",
       "9   524e19de-6602-4e3d-a619-42dbf9f74714       陈嘉仪\n",
       "10  0bcead85-3eda-456f-aa4d-518f7155c718       徐旖芊\n",
       "11  de49fea3-c4ea-4fa8-baf3-7dffa7fd4887       刘心如\n",
       "12  c2c37fec-623a-4b07-8509-bf0cfa02bbca        刘宇\n",
       "13  bd162714-85c3-4485-8b5a-e557e8bc02a4        李婷\n",
       "14  cac7ccd1-8b36-4957-ae4e-76a26c285ae1       黄智毅\n",
       "15  5adb05cd-466e-44b4-8f7b-70003db4e7d5       黄慧文\n",
       "16  ebc6190f-d734-4627-abeb-b743cffdfa81       张铭睿\n",
       "17  742ad08f-725c-4fb1-92b8-176565de4766        陈婷\n",
       "18  7ae16792-26e9-439f-b348-cc92ee8eac52       洪可凡\n",
       "19  1e4d1356-1499-4ca1-bc24-9d33af9e7200       卢继志\n",
       "20  5d2a20c7-9500-4e31-9d55-5d54a4d417ff       张梓乐"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用Pandas简化数据\n",
    "import pandas as pd\n",
    "# 返回facelist数据\n",
    "adf = pd.json_normalize(r_get_facelist.json()[\"persistedFaces\"])\n",
    "adf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>04152fc3-7ded-459f-8f93-35db58f2a41a</td>\n",
       "      <td>1.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>fb63b4b0-fb8d-4c6b-afc9-35951c291c9b</td>\n",
       "      <td>0.09950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a06c9e26-783b-4074-af26-01c531ef61da</td>\n",
       "      <td>0.09931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>de49fea3-c4ea-4fa8-baf3-7dffa7fd4887</td>\n",
       "      <td>0.09631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>174f9dfa-66e3-4f32-a25e-9b0d69ad61ae</td>\n",
       "      <td>0.09490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0229d72c-8cc1-4d2d-aca2-f39f1084864e</td>\n",
       "      <td>0.09432</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>c2c37fec-623a-4b07-8509-bf0cfa02bbca</td>\n",
       "      <td>0.09260</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1e4d1356-1499-4ca1-bc24-9d33af9e7200</td>\n",
       "      <td>0.09259</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0bcead85-3eda-456f-aa4d-518f7155c718</td>\n",
       "      <td>0.09235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>bd162714-85c3-4485-8b5a-e557e8bc02a4</td>\n",
       "      <td>0.09184</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  confidence\n",
       "0  04152fc3-7ded-459f-8f93-35db58f2a41a     1.00000\n",
       "1  fb63b4b0-fb8d-4c6b-afc9-35951c291c9b     0.09950\n",
       "2  a06c9e26-783b-4074-af26-01c531ef61da     0.09931\n",
       "3  de49fea3-c4ea-4fa8-baf3-7dffa7fd4887     0.09631\n",
       "4  174f9dfa-66e3-4f32-a25e-9b0d69ad61ae     0.09490\n",
       "5  0229d72c-8cc1-4d2d-aca2-f39f1084864e     0.09432\n",
       "6  c2c37fec-623a-4b07-8509-bf0cfa02bbca     0.09260\n",
       "7  1e4d1356-1499-4ca1-bc24-9d33af9e7200     0.09259\n",
       "8  0bcead85-3eda-456f-aa4d-518f7155c718     0.09235\n",
       "9  bd162714-85c3-4485-8b5a-e557e8bc02a4     0.09184"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回相似度数据\n",
    "bdf = pd.json_normalize(r_findsimilars.json())# 升级pandas才能运行\n",
    "bdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>persistedFaceId</th>\n",
       "      <th>userData</th>\n",
       "      <th>confidence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>04152fc3-7ded-459f-8f93-35db58f2a41a</td>\n",
       "      <td>Tim Cook</td>\n",
       "      <td>1.00000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>fb63b4b0-fb8d-4c6b-afc9-35951c291c9b</td>\n",
       "      <td>汤玲萍</td>\n",
       "      <td>0.09950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>a06c9e26-783b-4074-af26-01c531ef61da</td>\n",
       "      <td>杨悦聪</td>\n",
       "      <td>0.09931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>de49fea3-c4ea-4fa8-baf3-7dffa7fd4887</td>\n",
       "      <td>刘心如</td>\n",
       "      <td>0.09631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>174f9dfa-66e3-4f32-a25e-9b0d69ad61ae</td>\n",
       "      <td>丘天惠</td>\n",
       "      <td>0.09490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0229d72c-8cc1-4d2d-aca2-f39f1084864e</td>\n",
       "      <td>林嘉茵</td>\n",
       "      <td>0.09432</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>c2c37fec-623a-4b07-8509-bf0cfa02bbca</td>\n",
       "      <td>刘宇</td>\n",
       "      <td>0.09260</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1e4d1356-1499-4ca1-bc24-9d33af9e7200</td>\n",
       "      <td>卢继志</td>\n",
       "      <td>0.09259</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0bcead85-3eda-456f-aa4d-518f7155c718</td>\n",
       "      <td>徐旖芊</td>\n",
       "      <td>0.09235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>bd162714-85c3-4485-8b5a-e557e8bc02a4</td>\n",
       "      <td>李婷</td>\n",
       "      <td>0.09184</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                        persistedFaceId  userData  confidence\n",
       "0  04152fc3-7ded-459f-8f93-35db58f2a41a  Tim Cook     1.00000\n",
       "3  fb63b4b0-fb8d-4c6b-afc9-35951c291c9b       汤玲萍     0.09950\n",
       "4  a06c9e26-783b-4074-af26-01c531ef61da       杨悦聪     0.09931\n",
       "6  de49fea3-c4ea-4fa8-baf3-7dffa7fd4887       刘心如     0.09631\n",
       "1  174f9dfa-66e3-4f32-a25e-9b0d69ad61ae       丘天惠     0.09490\n",
       "2  0229d72c-8cc1-4d2d-aca2-f39f1084864e       林嘉茵     0.09432\n",
       "7  c2c37fec-623a-4b07-8509-bf0cfa02bbca        刘宇     0.09260\n",
       "9  1e4d1356-1499-4ca1-bc24-9d33af9e7200       卢继志     0.09259\n",
       "5  0bcead85-3eda-456f-aa4d-518f7155c718       徐旖芊     0.09235\n",
       "8  bd162714-85c3-4485-8b5a-e557e8bc02a4        李婷     0.09184"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#合并\n",
    "pd.merge(adf, bdf,how='inner', on='persistedFaceId').sort_values(by=\"confidence\",ascending = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5.删除人脸/人脸列表\n",
    "faceListId = \"list_004\" # 需要删除的人脸列表\n",
    "# 删除列表内人脸\n",
    "delete_face_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}/persistedfaces/{}\"\n",
    "assert subscription_key\n",
    "# 获取上面的persistedFaceId\n",
    "persistedFaceId = r_add_face.json()[\"persistedFaceId\"] \n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "# 注意requests请求为delete\n",
    "r_delete_face = requests.delete(delete_face_url.format(faceListId,persistedFaceId),headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除人脸列表\n",
    "delete_facelist_url = \"https://api-hjq.cognitiveservices.azure.com/face/v1.0/facelists/{}\"\n",
    "assert subscription_key\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': subscription_key,\n",
    "}\n",
    "r_delete_facelist = requests.delete(delete_facelist_url.format(faceListId),headers=headers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_face"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_delete_facelist"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
